package com.googlecode.openauth.web.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.ibatis.session.RowBounds;

import com.googlecode.openauth.web.config.CommonProperty;
import com.googlecode.openauth.web.dao.IMenuDao;
import com.googlecode.openauth.web.dao.IRoleDao;
import com.googlecode.openauth.web.dao.IRoleMenuDao;
import com.googlecode.openauth.web.entity.ActionPageBean;
import com.googlecode.openauth.web.entity.MenuInfo;
import com.googlecode.openauth.web.entity.RMenu;
import com.googlecode.openauth.web.entity.RoleInfo;
import com.googlecode.openauth.web.entity.UserInfo;
import com.googlecode.openauth.web.util.Util;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class MenuAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 8662992801656062256L;
	private int menuId ;
	private int[] menuIds;
	private String oper = "list";
	private IMenuDao menuDao;
	private String uuid;
	private String menuName;
	private int parent;
	private int prevMenu;
	private String menuDesc;
	private List<MenuInfo> list;
	private IRoleMenuDao roleMenuDao;
	private ActionPageBean<MenuInfo> actionPageBean;
	private int currentPageNo = 1;
	private int prevPageNo = 1;
	private int nextPageNo = 1;
	private int totalCount = 0;
	private int pageNum = CommonProperty.PAGENUM;
	private IRoleDao roleDao;

	public IRoleDao getRoleDao() {
		return roleDao;
	}

	public void setRoleDao(IRoleDao roleDao) {
		this.roleDao = roleDao;
	}

	public String list() throws Exception {
		MenuInfo menu = new MenuInfo();
		menu.setRoleId(this.getCurRoleId());
		menu.setParent(parent);
		//list = this.menuDao.getMenus(menu);
		list = menuDao.getAllMenus(menu, new RowBounds(0, pageNum));
		actionPageBean = new ActionPageBean<MenuInfo>();
		actionPageBean.setData(list);
		menu.setMenuId(menuId);
				List<MenuInfo> pmenu=	menuDao.getParentMenu(menu);
		if(null!=pmenu&&pmenu.size()==1)
		{
			prevMenu=pmenu.get(0).getParent();
		} 
		else
		{
			prevMenu=0;
		}
		
		return "success";
	}

	private UserInfo getCurUser(){
		ActionContext context = ActionContext.getContext();
		Map<String, Object> session = context.getSession();
		Object obj = session.get("user");
		UserInfo result = new UserInfo();
		result.setUserId(-1);
		if(obj == null){
			return result;
		}
		
		if(!(obj instanceof UserInfo)){
			return result;
		}
		
		result = (UserInfo) obj;
		return result;
	}
	
	private RoleInfo getCurRole(){
		RoleInfo result = new RoleInfo();
		result.setRoleId(-1);
		
		UserInfo userInfo = getCurUser();
		List<RoleInfo> roles = userInfo.getRoleInfos();
		
		if(roles == null || roles.isEmpty()){
			return result;
		}
		
		result = roles.get(0);
		return result;
	}
	
	private int getCurRoleId() {
		RoleInfo role = getCurRole();
		int curRoleId = role.getRoleId();
		return curRoleId;
	}

	public String getFirstPage() throws Exception {
		MenuInfo curRole = getCurMenu();
		int totalCount = menuDao.getAllMenusCount(curRole);
		actionPageBean = new ActionPageBean<MenuInfo>();
		actionPageBean.setTotalCount(totalCount);

		ActionPageBean<MenuInfo> pageBean = getPageBean();
		pageBean.setTotalCount(totalCount);
		pageBean.setPrevPageNo(1);
		pageBean.setCurrentPageNo(1);
		Util util = new Util();
		int maxPageNo = util.getMaxPageNo(pageBean);
		if (maxPageNo <= 1) {
			pageBean.setNextPageNo(1);
		} else {
			pageBean.setNextPageNo(2);
		}

		int offset = 0;
		int limit = pageBean.getPageNum();
		RowBounds rowBounds = new RowBounds(offset, limit);
		setPageBean(pageBean);
		List<MenuInfo> data = menuDao.getAllMenus(curRole, rowBounds);
		actionPageBean.setData(data);
		return SUCCESS;
	}
	private MenuInfo getCurMenu()
	{
		MenuInfo curMenu =new  MenuInfo();
		RoleInfo role=getCurRole();
		curMenu.setRoleId(role.getRoleId());
		curMenu.setParent(parent);
		return curMenu;
	}
	
	public String getPrevPage() throws Exception {
		MenuInfo curRole = getCurMenu();
		int totalCount = menuDao.getAllMenusCount(curRole);
		actionPageBean = new ActionPageBean<MenuInfo>();
		actionPageBean.setTotalCount(totalCount);

		ActionPageBean<MenuInfo> pageBean = getPageBean();
		pageBean.setTotalCount(totalCount);
		Util util = new Util();
		RowBounds rowBounds = util.calcPrevRowBounds(pageBean);
		setPageBean(pageBean);
		List<MenuInfo> data = menuDao.getAllMenus(curRole, rowBounds);
		actionPageBean.setData(data);
		return SUCCESS;
	}

	public String getNextPage() throws Exception {

		MenuInfo curRole = getCurMenu();
		int totalCount = menuDao.getAllMenusCount(curRole);
		actionPageBean = new ActionPageBean<MenuInfo>();
		actionPageBean.setTotalCount(totalCount);

		ActionPageBean<MenuInfo> pageBean = getPageBean();
		pageBean.setTotalCount(totalCount);
		Util util = new Util();
		RowBounds rowBounds = util.calcNextRowBounds(pageBean);
		setPageBean(pageBean);
		List<MenuInfo> data = menuDao.getAllMenus(curRole, rowBounds);
		actionPageBean.setData(data);
		return SUCCESS;
	}

	public String getLastPage() throws Exception {
		MenuInfo curRole = getCurMenu();
		int totalCount = menuDao.getAllMenusCount(curRole);
		actionPageBean = new ActionPageBean<MenuInfo>();
		actionPageBean.setTotalCount(totalCount);

		ActionPageBean<MenuInfo> pageBean = getPageBean();
		pageBean.setTotalCount(totalCount);
		Util util = new Util();
		int maxPageNo = util.getMaxPageNo(pageBean);
		pageBean.setCurrentPageNo(maxPageNo);
		pageBean.setNextPageNo(maxPageNo);

		if (maxPageNo <= 1) {
			pageBean.setPrevPageNo(1);
		} else {
			pageBean.setPrevPageNo(maxPageNo - 1);
		}
		int limit = pageBean.getPageNum();
		int offset = (maxPageNo - 1) * limit;
		RowBounds rowBounds = new RowBounds(offset, limit);
		setPageBean(pageBean);
		List<MenuInfo> data = menuDao.getAllMenus(curRole, rowBounds);
		actionPageBean.setData(data);
		return SUCCESS;
	}

	public String save() throws Exception {
		MenuInfo form = this.getForm();
		if (oper.equals("save")) {
			
			menuDao.addMenuInfo(form);
			int roleId = getCurRoleId();
			List<RoleInfo> rlist = new ArrayList<RoleInfo>();
			rlist = roleDao.getChildRolesLeaf2Root(roleId);
			if (rlist.size() == 0) {
				RoleInfo r = new RoleInfo();
				r.setRoleId(roleId);
				rlist.add(r);
			}
			for (RoleInfo role : rlist) {
				RMenu rmenu = new RMenu();
				rmenu.setMenuId(form.getMenuId());
				rmenu.setRoleId(role.getRoleId());
				this.roleMenuDao.addRoleMenu(rmenu);
			}

			resetForm();
			return "success";
		} else {
			menuDao.updateMenu(form);
			resetForm();
			this.menuId = 0;
			return "success";
		}
	}

	public String init() throws Exception {

		if (oper.equals("save")) {
			System.out.println(parent);
			return "new";
		} else {
			MenuInfo resourrce = menuDao.getMenuById(menuId);
			setForm(resourrce);
			return "update";
		}
	}

	public String delete() throws Exception {
		roleMenuDao.delRoleMenuByMenuId(menuIds[0]);
		menuDao.delMenuInfo(menuIds[0]);
		resetForm();
		return "success";
	}

	public void setForm(MenuInfo res) {
		uuid = res.getUuid();
		menuName = res.getMenuName();
		parent = res.getParent();
		menuDesc = res.getMenuDesc();
		menuId = res.getMenuId();
	}

	public MenuInfo getForm() {
		MenuInfo res = new MenuInfo();
		res.setMenuDesc(menuDesc);
		res.setMenuId(menuId);
		res.setMenuName(menuName);
		res.setParent(parent);
		return res;
	}

	public void resetForm() {
		uuid = "";
		menuName = "";
		// parent=-1;
		menuDesc = "";
		menuId = 0;
	}

	public int getMenuId() {
		return menuId;
	}

	public void setMenuId(int menuId) {
		this.menuId = menuId;
	}

	public int[] getMenuIds() {
		return menuIds;
	}

	public void setMenuIds(int[] menuIds) {
		this.menuIds = menuIds;
	}

	public String getOper() {
		return oper;
	}

	public void setOper(String oper) {
		this.oper = oper;
	}

	public IMenuDao getMenuDao() {
		return menuDao;
	}

	public void setMenuDao(IMenuDao menuDao) {
		this.menuDao = menuDao;
	}

	public String getUuid() {
		return uuid;
	}

	public void setUuid(String uuid) {
		this.uuid = uuid;
	}

	public String getMenuName() {
		return menuName;
	}

	public void setMenuName(String menuName) {
		this.menuName = menuName;
	}

	public int getParent() {
		return parent;
	}

	public void setParent(int parent) {
		this.parent = parent;
	}

	public String getMenuDesc() {
		return menuDesc;
	}

	public void setMenuDesc(String menuDesc) {
		this.menuDesc = menuDesc;
	}

	public List<MenuInfo> getList() {
		return list;
	}

	public void setList(List<MenuInfo> list) {
		this.list = list;
	}

	public ActionPageBean<MenuInfo> getActionPageBean() {
		return actionPageBean;
	}

	public void setActionPageBean(ActionPageBean<MenuInfo> actionPageBean) {
		this.actionPageBean = actionPageBean;
	}

	public ActionPageBean<MenuInfo> getPageBean() {
		ActionPageBean<MenuInfo> bean = new ActionPageBean<MenuInfo>();
		bean.setCurrentPageNo(currentPageNo);
		bean.setPrevPageNo(prevPageNo);
		bean.setNextPageNo(nextPageNo);
		bean.setTotalCount(totalCount);
		bean.setPageNum(pageNum);
		return bean;
	}

	public void setPageBean(ActionPageBean<MenuInfo> bean) {
		currentPageNo = bean.getCurrentPageNo();
		prevPageNo = bean.getPrevPageNo();
		nextPageNo = bean.getNextPageNo();
		totalCount = bean.getTotalCount();
	}

	public int getCurrentPageNo() {
		return currentPageNo;
	}

	public void setCurrentPageNo(int currentPageNo) {
		this.currentPageNo = currentPageNo;
	}

	public int getPrevPageNo() {
		return prevPageNo;
	}

	public void setPrevPageNo(int prevPageNo) {
		this.prevPageNo = prevPageNo;
	}

	public int getNextPageNo() {
		return nextPageNo;
	}

	public void setNextPageNo(int nextPageNo) {
		this.nextPageNo = nextPageNo;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public IRoleMenuDao getRoleMenuDao() {
		return roleMenuDao;
	}

	public void setRoleMenuDao(IRoleMenuDao roleMenuDao) {
		this.roleMenuDao = roleMenuDao;
	}

	public int getPrevMenu() {
		return prevMenu;
	}

	public void setPrevMenu(int prevMenu) {
		this.prevMenu = prevMenu;
	}
	

}
